// 
//  Copyright (C) 2009 Jacek Trubłajewicz
// 
//  This program is free software; you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation; either version 2 of the License, or
//  (at your option) any later version.
// 
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//  GNU General Public License for more details.
//  
//  You should have received a copy of the GNU General Public License
//  along with this program; if not, write to the Free Software
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
// 

using System;
using System.Collections.Generic;

namespace wxGadu
{
	/// <summary>
	/// Implementation of <c>List<T></c>
	/// with limited constant capacity.
	/// Adding new item to full list will cause 
	/// removal of the oldest item (the one on the tail
	/// of the list)
	/// </summary>
	/// <remarks>
	/// Added item will always be inserted at first (zero-based) positon on the list
	/// </remarks>
	public class LimitedList<T> : List<T>
	{
		private int capacity;
		
		/// <summary>
		/// Ctor
		/// </summary>
		/// <param name="capacity">
		/// A <see cref="System.Int32"/>
		/// Capacity of the list
		/// </param>
		/// <exception cref="ArgumentOutOfRangeException">
		/// <c>capacity</c> value is less than 1
		/// </exception>
		public LimitedList(int capacity): base(capacity)
		{
			if (capacity < 1)
				throw new ArgumentOutOfRangeException("Capacity value must be at least 1");
			
			this.capacity = capacity;
		}
		
		/// <summary>
		/// Add new item to the list
		/// </summary>
		/// <param name="item">
		/// A <see cref="T"/>
		/// </param>
		/// <remarks>
		/// Adding new item to full list will cause 
		/// removal of the oldest item (the one on the tail
		/// of the list).
		/// Added item will always be inserted at first (zero-based) positon on the list
		/// </remarks>
		public new void Add(T item)
		{
			if (this.Count < capacity)
				base.Insert(0, item);
			else
			{
				base.RemoveAt(capacity-1);
				base.Insert(0, item);
			}
		}
		
		/// <summary>
		/// Inserts new item
		/// </summary>
		/// <param name="index">
		/// A <see cref="System.Int32"/>
		/// </param>
		/// <param name="item">
		/// A <see cref="T"/>
		/// </param>
		/// <remarks>
		/// Added item will always be inserted at first (zero-based) positon on the list
		/// </remarks>
		public new void Insert(int index, T item)
		{
			this.Add(item);
		}
		
		/// <summary>
		/// Inserts collection of items to the list
		/// </summary>
		/// <param name="index">
		/// A <see cref="System.Int32"/>
		/// </param>
		/// <param name="collection">
		/// A <see cref="IEnumerable"/>
		/// </param>
		/// <remarks>
		/// NOT IMPLEMENTED
		/// </remarks>
		/// <exception cref="NotImplementedException">
		/// Thrown after method is called
		/// </exception>
		private new void InsertRange(int index, IEnumerable<T> collection)
		{
			throw new NotImplementedException();
		}
	}
}
